/**
 * Copyright (C), 2015-2019, XXX有限公司
 * FileName: AliPayChannelProxy
 * Author:   zhouheng
 * Date:     2019/5/15 11:44
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.star.pay.service.core.processor.channel.alipay;

import com.star.pay.service.core.common.PayChannelEnum;
import com.star.pay.service.core.common.PaymentResponseEnum;
import com.star.pay.service.core.config.PaymentConfig;
import com.star.pay.service.core.processor.channel.BasePayChannelProxy;
import com.star.pay.service.core.processor.channel.PayChannelProxy;
import com.star.pay.service.core.processor.context.AliPayContext;
import com.star.pay.service.core.processor.context.PaymentContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author zhouheng
 * @create 2019/5/15
 * @since 1.0.0
 */
@Component
@Slf4j
public class AliPayChannelProxy extends BasePayChannelProxy {

    @Autowired
    PaymentConfig paymentConfig;

    @Override
    public String getPayChannel() {
        return PayChannelEnum.ALI_PAY.getCode();
    }

    /**
     * @description: 支付参数校验
     * @date: 2019/5/15 23:02
     * @author:zhouheng
     */
    @Override
    public boolean preCheck(PaymentContext paymentContext) {
        AliPayContext aliPayContext = (AliPayContext) paymentContext;
        if(StringUtils.isBlank(aliPayContext.getOutTradeNo())){
            paymentContext.setResCode(PaymentResponseEnum.SYS_PARAM_NOT_RIGHT, "商户订单号不能为空");
            return false;
        }
        if(StringUtils.isBlank(aliPayContext.getSubject())){
            paymentContext.setResCode(PaymentResponseEnum.SYS_PARAM_NOT_RIGHT, "商品名称不能为空");
            return false;
        }
        if(aliPayContext.getTotalFee()==null){
            paymentContext.setResCode(PaymentResponseEnum.SYS_PARAM_NOT_RIGHT, "交易金额不能为空");
            return false;
        }

        return true;
    }

    /**
     * @description: 支付宝只需要将参数拼接成一个html表单。 在前段页面展示出来即可。用户在该表单中支付完成后有个回调。
     * @date: 2019/5/15 23:02
     * @author:zhouheng
     */
    @Override
    public boolean paymentProcess(PaymentContext paymentContext) {
        AliPayContext aliPayContext = (AliPayContext) paymentContext;

        try {
            //把请求参数打包成数组
            SortedMap<String, Object> sParaTemp =new TreeMap<String, Object>();
            sParaTemp.put("service", paymentConfig.getAli_service());
            sParaTemp.put("partner", paymentConfig.getAli_partner());
            sParaTemp.put("seller_email", paymentConfig.getSeller_email());
            sParaTemp.put("seller_id", paymentConfig.getSeller_id());
            sParaTemp.put("_input_charset", paymentConfig.getInput_charset());
            sParaTemp.put("payment_type", 1);
            sParaTemp.put("it_b_pay", paymentConfig.getIt_b_pay());
            sParaTemp.put("notify_url", paymentConfig.getNotify_url());
            sParaTemp.put("return_url",paymentConfig.getReturn_url());
            sParaTemp.put("out_trade_no",aliPayContext.getOutTradeNo());
            sParaTemp.put("subject", aliPayContext.getSubject());
            sParaTemp.put("total_fee", aliPayContext.getTotalFee().doubleValue());

            String strPara = AlipayRequest.buildRequest(sParaTemp, "get", "确认",paymentConfig);
            log.info("【支付宝落单】支付宝请求html："+strPara);
            aliPayContext.setHtmlStr(strPara);
            aliPayContext.setResCode(PaymentResponseEnum.SUCCESS,"");

            return true;
        } catch (Exception e) {
            log.error("【支付宝落单】支付宝下单异常：");
            aliPayContext.setResCode(PaymentResponseEnum.PAYMENT_PROCESSOR_FAILED,e.getMessage());
            return false;
        }
    }
}